<template>
  <el-dialog v-model="visible" title="核销积分" width="860">
    <el-row>
      <el-form :inline="true" :model="queryData" ref="formQueryRef">
        <el-form-item label="手机号码" prop="mobile">
          <el-input
            placeholder="请输入手机号码"
            v-model.number="queryData.mobile"
            clearable
          />
        </el-form-item>
        <el-form-item label="姓名" prop="nickname">
          <el-input
            placeholder="请输入姓名"
            v-model.trim="queryData.nickname"
            clearable
          />
        </el-form-item>
      </el-form>
      <div class="flex-right">
        <el-button type="primary" :icon="RefreshLeft" @click="resetForm">
          重置
        </el-button>
        <el-button
          type="primary"
          :icon="Search"
          class="ml20"
          @click="initData"
          v-enter="initData"
        >
          搜索
        </el-button>
      </div>
    </el-row>
    <el-table
      stripe
      border
      ref="tableRef"
      v-loading="loading"
      :data="tableData"
      key="id"
    >
      <el-table-column type="index" label="序号" width="55" />
      <el-table-column prop="username" label="工号" />
      <el-table-column prop="mobile" label="手机号码" />
      <el-table-column prop="nickname" label="姓名" />
      <el-table-column prop="roles" label="角色" />
      <el-table-column prop="status" label="状态">
        <template v-slot:default="{ row }">
          <el-text :type="row.status ? 'danger' : 'success'">
            {{ row.status === 1 ? "锁定" : "正常" }}
          </el-text>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="操作" width="80" align="center">
        <template v-slot:default="{ row }">
          <el-button
            text
            type="danger"
            size="small"
            :disabled="row.status === 1"
            @click="onReversalStatus(row)"
          >
            核销
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      class="mt-4"
      :total="total"
      :page-size="queryData.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, prev, pager, next"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-dialog>
</template>
<script setup>
import { nextTick, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Search, RefreshLeft } from "@element-plus/icons-vue";
import apiLabel from "@/api/label.js";
import apiStaff from "@/api/staff.js";

const props = defineProps({});
const visible = ref(false);
const tableData = ref([]);
const tableRef = ref();
const formQueryRef = ref();
const total = ref(0);
const cancelParams = ref({});

const queryData = ref({
  pageNo: 1,
  pageSize: 10,
});
const loading = ref(false);
const emits = defineEmits(["success"]);

const initData = async () => {
  loading.value = true;
  try {
    const res = await apiStaff.page({
      ...queryData.value,
    });
    tableData.value = res.data?.list || [];
    total.value = res.data?.total;
  } finally {
    loading.value = false;
  }
};

const handleCurrentChange = (val) => {
  queryData.value.pageNo = val;
  initData();
};
const handleSizeChange = (val) => {
  queryData.value.pageSize = val;
  initData();
};

const resetForm = () => {
  formQueryRef.value.resetFields();
};

const onReversalStatus = (row) => {
  ElMessageBox.confirm(
    `确定给工号<span style='color:red;'>${row.username}</span>核销积分吗？`,
    "温馨提示",
    {
      type: "warning",
      dangerouslyUseHTMLString: true,
      confirmButtonText: "确定",
      cancelButtonText: "取消",
    }
  ).then(() => {
    apiLabel
      .reversalStatus({
        id: cancelParams.value.id,
        status: 1,
        userid: row.id,
      })
      .then((res) => {
        if (res.data) {
          ElMessage.success(res.msg || "核销成功");
          handleCurrentChange(1);
          visible.value = false;
          emits("success");
        } else {
          // ElMessage.error(res.msg || "核销失败");
        }
      });
  });
};

const openDialog = (row) => {
  visible.value = true;
  tableData.value = [];
  cancelParams.value = row;
  nextTick(() => {
    handleCurrentChange(1);
    resetForm();
  });
};

defineExpose({
  openDialog,
});
</script>
